package primetest.impl.converter;



import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import primetest.dto.DoctorDto;



@FacesConverter(value = "DoctorConverter")
public class DoctorConverter implements Converter {
    @Override
    public Object getAsObject(FacesContext context, UIComponent component,
            String str) {
    	Object ret = null;
        if (component instanceof PickList) {
            Object dualList = ((PickList) component).getValue();
            DualListModel<DoctorDto> dl = (DualListModel<DoctorDto>) dualList;
            for (DoctorDto d : dl.getSource()) {
                if (str.equals(d.getId())) {
                    ret = d;
                    break;
                }
            }
            for (DoctorDto d : dl.getTarget()) {
                if (str.equals(d.getId())) {
                    ret = d;
                    break;
                }
            }
        }
        return ret;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component,
            Object obj) {
        return ((DoctorDto) obj).getId();
    }

}
